feat: add balance metadata for snap datasource [DO NOT MERGE]#9564
feat: add balance metadata for snap datasource [DO NOT MERGE]#9564stanleyyconsensys wants to merge 2 commits into
Conversation
| [assetId: string]: { | ||
| amount: string; | ||
| unit: string; | ||
| metadata?: Record<string, Json>; |
There was a problem hiding this comment.
add metadata into snap balance update event payload type
There was a problem hiding this comment.
Do we want type enforced payloads?
E.g. a typed descriminated union?
There was a problem hiding this comment.
Or maybe we can enforce a schema for runtime validation & parsing?
There was a problem hiding this comment.
i think in future
some snap may still using SNAP path to sync data, those data will not able to predict
adding validation may also adding a complexity on how snap ship data to controller
and it require bump up to controller when the data sharp change..., so i will suggest leave it as this (should align with keyring API)
| accountAssets ??= {}; | ||
| accountAssets[assetId as Caip19AssetId] = { | ||
| amount: balance.amount, | ||
| ...(balance.metadata ? { metadata: balance.metadata } : {}), |
There was a problem hiding this comment.
when persist balance data via event, we persist metadata together if any
type AssetBalance already ready to store metadata
|
|
||
| // Step 2: Get balances for those specific assets | ||
| const balances: Record<CaipAssetType, Balance> = | ||
| const balances: Record<CaipAssetType, Balance & { metadata?: Json }> = |
There was a problem hiding this comment.
mock keyring type
| if (accountBalances) { | ||
| (accountBalances as Record<string, unknown>)[assetId] = { | ||
| amount: balance.amount, | ||
| ...(balance.metadata ? { metadata: balance.metadata } : {}), |
There was a problem hiding this comment.
when persist balance data via keyring API get balance, we persist metadata together if any
Prithpal-Sooriya
left a comment
There was a problem hiding this comment.
Nice mock impl!
|
@metamaskbot publish-preview |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit a3e3158. Configure here.
| accountAssets ??= {}; | ||
| accountAssets[assetId as Caip19AssetId] = { | ||
| amount: balance.amount, | ||
| ...(balance.metadata ? { metadata: balance.metadata } : {}), |
There was a problem hiding this comment.
Snap metadata dropped on amount updates
Medium Severity
Balance entries written here only include metadata when the incoming snap payload provides it. Later amount-only updates still replace the whole per-asset balance object under merge, so previously stored metadata in controller state is removed even though the amount update was successful.
Additional Locations (1)
Reviewed by Cursor Bugbot for commit a3e3158. Configure here.
|
Preview builds have been published. Learn how to use preview builds in other projects. Expand for full list of packages and versions. |


Explanation
References
Checklist
Note
Low Risk
Additive, optional field on balance objects in a single data source; no auth or breaking contract changes in this diff.
Overview
Snap balances can now carry optional per-asset metadata through the assets pipeline when they come from keyring snaps.
SnapDataSourceextends theAccountsController:accountBalancesUpdatedpayload typing with optionalmetadataon each balance entry, and copies that field intoassetsBalancewhen handling live snap updates and when mappinggetAccountBalancesresults during fetch. Metadata is only attached when the snap provides it; amounts are unchanged.Reviewed by Cursor Bugbot for commit a3e3158. Bugbot is set up for automated code reviews on this repo. Configure here.